package com.botarena.client;

import com.botarena.client.account.AccountViewer;
import com.botarena.client.admin.AdminViewer;
import com.botarena.client.battle.BattleViewer;
import com.botarena.client.bot.BotViewer;
import com.botarena.client.contest.ContestViewer;
import com.botarena.shared.UserInfo;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Klasa sluząca to zarzadzania zawartoscia stron. Laduje strony.
 * 
 * @author wirenth
 * 
 */
public class ContentManager {

	private static UserInfo userInfo;
	private static RightMenu rightMenu;

	public static void changeSiteContent(String token, final UserInfo userInfo,
			final RightMenu rightMenu) {
		ContentManager.userInfo = userInfo;
		ContentManager.rightMenu = rightMenu;

		if (token.equals(SiteTokens.RANKING)) {
			ContentManager.changeSiteContent(ContestViewer
					.createRank(rightMenu));
		} else if (token.equals(SiteTokens.ABOUT)) {
			ContentManager.changeSiteContent(AboutViewer.createAboutPage());
		} else if (token.equals(SiteTokens.HOME) || token.equals("")) {
			ContentManager.changeSiteContent(HomeViewer.createHomePage(
					userInfo, rightMenu));
		} else if (userInfo.hasAccount()) {
			if (token.equals(SiteTokens.MY_BOTS)) {
				ContentManager.changeSiteContent(BotViewer.createMyBotsPage(
						userInfo, rightMenu));
			} else if (token.startsWith(SiteTokens.BOT_HOME)) {
				String botKey = token.substring(token.indexOf('?') + 1);
				ContentManager.changeSiteContent(BotViewer.createBotPage(
						botKey, rightMenu));
			} else if (token.startsWith(SiteTokens.USER_HOME)) {
				String userKey = token.substring(token.indexOf('?') + 1);
				ContentManager.changeSiteContent(AccountViewer.createUserPage(
						userKey));
			} else if (token.equals(SiteTokens.ACCOUNT)) {
				ContentManager.changeSiteContent(AccountViewer
						.createAccountPage(userInfo));
			} else if (token.equals(SiteTokens.BATTLES)) {
				ContentManager.changeSiteContent(BattleViewer
						.createBattlePanel(rightMenu, null));
			} else if (token.startsWith(SiteTokens.BATTLES)) {
				String key = token.substring(token.indexOf('?') + 1);
				ContentManager.changeSiteContent(BattleViewer
						.createBattlePanel(rightMenu, key));
			} else if (token.startsWith(SiteTokens.BATTLE_PREV)) {
				String key = token.substring(token.indexOf('?') + 1);
				ContentManager.changeSiteContent(BattleViewer
						.createBattlePreview(key));
			} else if (token.equals(SiteTokens.ADMIN) && userInfo.isAdmin()) {
				ContentManager
						.changeSiteContent(AdminViewer.createAdminPanel());
			} else {
				ContentManager.changeSiteContent(new Label(
						"Sorry, access denied!"));
			}
		} else {
			ContentManager.changeSiteContent(new Label(
					"Sorry, access denied! Please sign in."));
		}

	}

	public static void reloadContent() {
		reloadContent(ContentManager.userInfo, ContentManager.rightMenu);
	}

	public static void reloadContent(final UserInfo userInfo,
			final RightMenu rightMenu) {
		String locationToken = Location.getHash();
		if (!locationToken.equals(""))
			locationToken = locationToken.substring(1);
		changeSiteContent(locationToken, userInfo, rightMenu);
	}

	private static void changeSiteContent(Widget w) {

		RootPanel.get("content").clear();
		RootPanel.get("content").add(w);
	}

}
